package screens;

import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import runnables.Stoppable;
import customfields.AnimatedGIFField;

public class DownloadingPopupScreen extends PopupScreen implements FieldChangeListener{

    //statics ------------------------------------------------------------------

    private AnimatedGIFField animation = null;
    private LabelField message = null;
    
    // new
    private HorizontalFieldManager buttonManager = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
    private ButtonField cancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK);
    private Stoppable runningThis = null;
    
 // cancel constructor
    private DownloadingPopupScreen(final Runnable runThis, String text) {
        super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
        if ( runThis instanceof Stoppable ) {
            runningThis = (Stoppable) runThis;
        } else {
            runningThis = null;
        }
        GIFEncodedImage ourAnimation = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource("cycle.agif");
        animation = new AnimatedGIFField(ourAnimation, Field.FIELD_HCENTER);
        this.add(animation);
        message = new LabelField(text, Field.FIELD_HCENTER);
        this.add(message);
        if ( runThis instanceof Stoppable ) {
            // This would seem to be long enough to need a hide button as well...
            runningThis = (Stoppable) runThis;
            buttonManager.add(cancelButton);
            cancelButton.setChangeListener(this);
            //buttonManager.add(hideButton);
            //hideButton.setChangeListener(this);
            this.add(buttonManager);
           // hideButton.setFocus();
        } else {
            runningThis = null;
        }
    }


    private DownloadingPopupScreen(String text) {
        super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
        GIFEncodedImage ourAnimation = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource("cycle.agif");
        animation = new AnimatedGIFField(ourAnimation, Field.FIELD_HCENTER);
        this.add(animation);
        message = new LabelField(text, Field.FIELD_HCENTER);
        this.add(message);
    }
    

    // with cancel method 
    
    public static void showScreenAndWait(final Runnable runThis, String text) {
        final DownloadingPopupScreen thisScreen = new DownloadingPopupScreen(runThis, text);
        Thread threadToRun = new Thread() {
            public void run() {
                // First, display this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().pushScreen(thisScreen);
                    }
                });
                // Now run the code that must be executed in the Background
                try {
                    runThis.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                    throw new RuntimeException("Exception detected while waiting: " + t.toString());
                }
                // Now dismiss this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                    	UiApplication.getUiApplication().popScreen(thisScreen);
                    	if (!Controller.getInstance().getDownloadMessage().equals("")){
                    		FileDownloadScreen.showError(Controller.getInstance().getDownloadMessage());
                    		Controller.getInstance().setDownloadMessage("");
                    	}
                    	if(!Controller.getInstance().getCommentMessage().equals("")){
                    		GroupMessagesScreen.showError(Controller.getInstance().getCommentMessage());
                    		Controller.getInstance().setCommentMessage("");
                    	}
                    }
                });
            }
        };
        threadToRun.start();
    }


    public static void showScreenAndWait2(final Runnable runThis, String text) {
        final DownloadingPopupScreen thisScreen = new DownloadingPopupScreen(runThis, text);
        Thread threadToRun = new Thread() {
            public void run() {
                // First, display this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().pushScreen(thisScreen);
                    }
                });
                // Now run the code that must be executed in the Background
                try {
                    runThis.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                    throw new RuntimeException("Exception detected while waiting: " + t.toString());
                }
                // Now dismiss this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                    	UiApplication.getUiApplication().popScreen(thisScreen);
                    	if (!Controller.getInstance().getDownloadMessage().equals("")){
                    		FileDownloadScreen.showError(Controller.getInstance().getDownloadMessage());
                    	}
                    }
                });
            }
        };
        threadToRun.start();
    }
    
	public void fieldChanged(Field field, int context) {
        if ( field == cancelButton ) {
            runningThis.stop();
        }
	}

}




